package com.ship.control.impl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;

import com.ship.control.AbstractAdminFormController;
import com.ship.entity.ShipInfo;
import com.ship.entity.ShipMaterial;
import com.ship.entity.ShopMaterial;
import com.ship.entity.User;
import com.ship.service.ShipService;

/**
 * 船方自购物料编辑
 * @author wqf
 * @version  Nov 23, 2011 1:10:42 PM
 */
public class ShipShopMaterialEditController extends AbstractAdminFormController{

	public ShipShopMaterialEditController(){
		this.setCommandClass(ShopMaterial.class);
	}
		
	protected void addSubmit(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors) throws Exception {
		ShopMaterial shopMaterial=(ShopMaterial)command;
		this.shipService.editShopMaterial(shopMaterial);
		this.pt(response, "<script>window.parent.afterSubmit()</script>");		
	}	
	
	protected void modifySubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{		
		ShopMaterial shopMaterial = (ShopMaterial)command;
		this.shipService.editShopMaterial(shopMaterial);
		this.pt(response, "<script>window.parent.afterSubmit()</script>");
	}
		
	protected Object getCommandDataForModify(HttpServletRequest request){
		Long id=Long.valueOf(request.getParameter("id"));
		ShopMaterial shopMaterial = this.shipService.getShopMaterialById(id);
		if(null==shopMaterial.getMaterial())
			shopMaterial.setMaterial(new ShipMaterial());
		return shopMaterial;
	}
	
	protected Object getCommandDataForAdd(HttpServletRequest request) {
		ShopMaterial shopMaterial = new ShopMaterial();
		String tid = request.getParameter("shipInfoId");
		if(null!=tid){
			ShipInfo shipInfo = this.shipService.getShipInfoById(Long.parseLong(tid));
			shopMaterial.setShipInfo(shipInfo);
//			User user = new User();
//			user.setId(1l);
//			shopMaterial.setUser(user);
		}
		return shopMaterial;
	}
	
	
	public void pt(HttpServletResponse response,String text){
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write(text);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	
	public ShipService shipService;

	public void setShipService(ShipService shipService) {
		this.shipService = shipService;
	}

	
}
